﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
using System.Data.Objects;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblAttributeDAL:ImtblAttribute
    {
        HKEA_CMSEntities model;
        mtblAttribute attribute;
        public mtblAttributeDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblAttributeDAL GetInstance()
        {
            return new mtblAttributeDAL();
        }
        public int InsertENT(mtblAttribute ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblAttributes.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblAttribute> GetListByENT(mtblAttribute ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblAttributes.Where(a => a.AttributeUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.AttributeUID > 0)
                result = result.Where(a => a.AttributeUID == ent.AttributeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<AttributeDetail> GetAllByENT(mtblAttribute ent)
        {
            //TODO 分析这两条语句的区别
            //var result = from attribute in model.mtblAttributes
            //             where attribute.Deleted == ent.Deleted
            //             select new AttributeDetail
            //             {
            //                 FullName = (attribute.Attribute_Eng != null ? attribute.Attribute_Eng : string.Empty + " " + attribute.Attribute_Chi != null ? attribute.Attribute_Chi : string.Empty),
            //                 AttributeUID = attribute.AttributeUID,
            //                 Attribute_Chi = attribute.Attribute_Chi,
            //                 Attribute_Eng = attribute.Attribute_Eng
            //             };

            var result = from attribute in model.mtblAttributes
                         where attribute.Deleted == ent.Deleted
                         let eng = attribute.Attribute_Eng != null ? attribute.Attribute_Eng : string.Empty
                         let chi=attribute.Attribute_Chi!=null?attribute.Attribute_Chi: string.Empty
                         select new AttributeDetail
                         {
                             FullName = (eng +" "+chi),
                             AttributeUID = attribute.AttributeUID,
                             Attribute_Chi = attribute.Attribute_Chi,
                             Attribute_Eng = attribute.Attribute_Eng
                         };
           // var str = (result as ObjectQuery<AttributeDetail>).ToTraceString();
            if (ent.AttributeUID > 0)
                result = result.Where(a=>a.AttributeUID==ent.AttributeUID);
            return result.ToList();
        }

        public List<mtblAttribute> GetListByUIDList(List<int> uidList)
        {
            var result = model.mtblAttributes.Where(a => a.AttributeUID > 0).Where(a => a.Deleted == 0)
                .Where(a => uidList.Contains(a.AttributeUID));
            return result.ToList();
        }

        public List<mtblAttribute> GetListByENT(mtblAttribute ent)
        {
            var result = model.mtblAttributes.Where(a => a.AttributeUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.AttributeUID > 0)
                result = result.Where(a => a.AttributeUID == ent.AttributeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(mtblAttribute ent)
        {
            var result = model.mtblAttributes.Where(a => a.AttributeUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.AttributeUID > 0)
                result = result.Where(a => a.AttributeUID == ent.AttributeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblAttribute ent)
        {
            attribute = model.mtblAttributes.Where(a =>a.AttributeUID ==ent.AttributeUID).FirstOrDefault();
            if (attribute != null)
            {
                attribute.Attribute_Chi = ent.Attribute_Chi;
                attribute.Attribute_Eng = ent.Attribute_Eng;
                attribute.Deleted = ent.Deleted;
                attribute.LastModifyBy = ent.LastModifyBy;
                attribute.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblAttribute ent)
        {
            attribute = model.mtblAttributes.Where(a => a.AttributeUID == ent.AttributeUID).FirstOrDefault();
            if (attribute != null)
            {
                model.mtblAttributes.DeleteObject(attribute);
            }
            return model.SaveChanges();
        }
    }
}
